
local func = {}
local data = load_excel('存档数据.xlsx','天赋页存档')
local keys = ac.register_dzscore3('TF',40,1)
local map = {}
local lock = {}     --前置激活连接

func['A10'] = function(hero,data,level)
    local skill = hero:add_skill('弹射光球')
    skill.data1 = (data.data1 or 0) * level
    skill.data2 = (data.data2 or 0) * level
end

func['B10'] = function(hero,data,level)
    local skill = hero:add_skill('剑气纵横')
    skill.data1 = (data.data1 or 0) * level
    skill.data2 = (data.data2 or 0) * level
end

func['C9'] = function(hero,data,level)
    local skill = hero:add_skill('流星雨')
    skill.data1 = (data.data1 or 0) * level
    skill.data2 = (data.data2 or 0) * level
end

func['V3'] = function(hero,data,level)

end

func['V4'] = function(hero,data,level)
    hero:event '单位-造成伤害前'(function(_,_,target,dmg)
        if target:is_type('精英') then
            dmg:mul(data.data1*level)
        end
    end)
end

func['V5'] = function(hero,data,level)
    hero:event '单位-造成伤害前'(function(_,_,target,dmg)
        if target:is_type('Boss') then
            dmg:mul(data.data1*level)
        end
    end)
end


--[解析数据]
for id,info in ipairs(data) do
    local name = info.name or info['对应']
    local link = info.link and split_string(info.link,',') or {}
    info.title = info['名称']
    info.id = id
    info.code = keys[id]
    info.link = link
    info.tip = format_obj(info,info['效果'])
    if func[name]==nil then
        info['属性'] = cd_unpack_attrs2(info['效果'],'data')
    end

    map[name] = info
    if info.code==nil then
        log.error('没有注册天赋树存档',id,name)
    end
    
    --[添加前置连接]
    for a,key in ipairs(link) do
        lock[key] = true
    end
end

--[排序]
local load_data = table.copy(data)
table.sort(load_data,function(a,b)
    return a.x < b.x
end)

ac.game:event '玩家-选择英雄完毕'(function(_,player)
    player:notify('玩家-加载天赋树属性',player)
end)

ac.game:event '玩家-加载通关存档'(function(_,player)
    player:notify('玩家-加载天赋树属性',player)
end)

ac.game:event '云消息-获取天赋点'(function(_,player,point)
    player:set_data('云存档天赋点',point)
    player:notify('玩家-加载天赋树属性',player)
end)

ac.game:event '玩家-加载天赋树属性'(function(_,player)
    local hero = player.hero
    local count = player:get_data('加载次数',0) + 1
    local point = player:get_data('云存档天赋点',0)
    player:set_data('加载次数',count)
    if count==4 then
        local list = {}
        local allow = {}
        local sum = 0
        local use = 0
        for _,info in ipairs(load_data) do
            local id = info.id
            local name = info.name or info['对应']
            local lv = player:dz_get(info.code)
            local link = info.link or {}
            local need = (lv+1)*lv/2 * 50 --需要的点数

            if lv>0 and (lock[name]==nil or allow[name]) and point>=need and sum>=(info['限制等级'] or 0) then
                list[id] = lv
                sum = sum + lv
                use = use + need
                point = point - need
                --[激活后续]
                for a,key in ipairs(link) do
                    allow[key] = true
                    print('激活节点',key)
                end
                for _,da in ipairs(info['属性'] or {}) do
                    local k = da[1]
                    local v = da[2]
                    local n = info[v]
                    hero:add(k,n*lv)
                end
                if func[name] then
                    func[name](hero,info,lv)
                end
            else
                list[id] = 0
            end
        end
        player:set_data('天赋树-天赋点',point)
        player:set_data('天赋树-总等级',sum)
        player:set_data('天赋树-使用点',use)
        player:set_data('天赋树-节点等级',list)
        player:set_data('天赋树-节点允许',allow)
    end
end)


ac.game:event '玩家-天赋树加点'(function(_,player,name,count,hide)
    local list = player:get_data('天赋树-节点等级') or {} --保存等级
    local allow = player:get_data('天赋树-节点允许') or {} --保存等级
    local sum = player:get_data('天赋树-总等级') or 0
    local use = player:get_data('天赋树-使用点') or 0
    local point = player:get_data('天赋树-天赋点') or 0
    local info = map[name]
    local link = info.link or {}
    local count = count or 1
    local level = list[info.id] or 0
    local minlv = info['限制等级'] or 0
    
    if sum<minlv or (lock[name] and allow[name]==nil) then
        return
    end
    for a=1,count do
        local need = (level + 1) * 50 --需要的点数
        --加点条件
        if level==info['等级'] or point<need then
            break
        end
        --[激活后续]
        for a,key in ipairs(link) do
            allow[key] = true
        end
        level = level + 1
        point = point - need
        use = use + need
        sum = sum + 1
        list[info.id] = level
        player:dz_set(info.code,level)
        
    end
    player:set_data('天赋树-总等级',sum)
    player:set_data('天赋树-使用点',use)
    player:set_data('天赋树-天赋点',point)
    --保存数据
    list[info.id] = level
end)


ac.sync:event '同步-天赋树加点'(function(_,player,name,count)
    if player:get_data('天赋树-天赋点')==nil then
        return
    end
    player:notify('玩家-天赋树加点',player,name,count)
end)


ac.game:event '游戏-加载完成'(function(_,hard)
    ac.each_player(function(player)
        player:send_cloud('获取天赋点')
    end)
end)

